﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DBQLTV;
namespace QuanLyThuVien
{
    public partial class frm_XoaDocGia : Form
    {
        private frmMain mainForm = null;

        DocGiaBUS docGiaBUS = new DocGiaBUS();
        List<DOCGIA> listDG;
        int TongNo = 0;
        public frm_XoaDocGia(Form callingForm)
        {
            mainForm = callingForm as frmMain;
            InitializeComponent();
        }
        private void frm_XoaDocGia_Load(object sender, EventArgs e)
        {
            listDG = docGiaBUS.GetListDocGia();
            foreach (DOCGIA dg in listDG)
            {
                comboBox_maDG.Items.Add(dg.MaDG);
            }
        }
        private void UpdateThongTin()
        {
            int maDG = int.Parse(comboBox_maDG.Text);
            tb_HoTen.Text = docGiaBUS.GetDocGia(maDG).Ten;
            tb_diaChi.Text = docGiaBUS.GetDocGia(maDG).DiaChi;
            tb_email.Text = docGiaBUS.GetDocGia(maDG).Email;
            tb_loaiDG.Text = docGiaBUS.GetLoaiDocGia(maDG).TenLoaiDG;
            tb_ngayLapThe.Text = docGiaBUS.GetDocGia(maDG).NgayLapThe.Value.ToShortDateString();
            tb_ngaySinh.Text = docGiaBUS.GetDocGia(maDG).NgaySinh.ToShortDateString();
            tb_slSachDangMuon.Text = docGiaBUS.GetSoLuongCuonSachDangMuon(maDG).ToString();
            tb_tongNo.Text = docGiaBUS.GetDocGia(maDG).TongNo.ToString();
        }
        private void button_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void button_Xoa_Click(object sender, EventArgs e)
        {
            int MaDG = int.Parse(comboBox_maDG.SelectedItem.ToString());

            TongNo = docGiaBUS.GetDocGia(MaDG).TongNo.Value;
            int SachDangMuon = docGiaBUS.GetSoLuongCuonSachDangMuon(MaDG);
            if (TongNo != 0 || SachDangMuon != 0) MessageBox.Show("Độc giả đang nợ hoặc đang mượn sách!");
            else
            {
                DialogResult dresult = MessageBox.Show("Xóa độc giả có mã độc giả này?", "Cảnh báo", MessageBoxButtons.YesNo);
                if (dresult == DialogResult.Yes)
                {
                    new PhieuMuonSachBUS().DeletePhieuMuonSach(MaDG);
                    List<int> listMaPhieuTra = new PhieuTraSachBUS().GetListPhieuTraSach(MaDG);
                    if (listMaPhieuTra != null)
                        foreach (int temp in listMaPhieuTra)
                        {
                            new CTPhieuTraSachBUS().DeleteCTPhieuTraSach(temp);
                        }
                    new PhieuTraSachBUS().DeletePhieuTraSach(MaDG);
                    docGiaBUS.DeleteDocGia(MaDG);
                    MessageBox.Show("Xóa thành công!");
                    mainForm.LoadDataGrid_DSDocGia();
                }
                this.Close();
            }

        }
        private void comboBox_maDG_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                if (!comboBox_maDG.Items.Contains(int.Parse(comboBox_maDG.Text)))
                    MessageBox.Show("Mã độc giả không tồn tại!", "Lỗi");
                else UpdateThongTin();
        }
        private void comboBox_maDG_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateThongTin();
        }
    }
}
